package net.telesing.tcmc.back.action;

import net.telesing.tcmc.back.logic.UserService;
import net.telesing.tcmc.back.model.MenuModel;
import net.telesing.tcmc.back.model.UserModel;
import net.telesing.tcmc.common.Constants;
import net.telesing.tcmc.common.LogicException;
import net.telesing.tcmc.core.web.Ajax;
import net.telesing.tcmc.core.web.AjaxBean;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by 一凡 on 2015/11/3.
 */
@At("/user")
@IocBean
public class UserAction {
    /**
     * userService
     */
    @Inject
    private UserService userService;
    /**
     * log
     */
    private static final Logger log = LogManager.getLogger(UserAction.class);
    /**
     *TAG
     */
    private static final String TAG = "UserAction";

    /**
     * 跳到登录页面
     */
    @At
    @Filters
    @Ok("jsp:pages.back.user.user_login")
    public void doLogin(@Param("jumpType") int jumpType) {
        log.info("user > toLogin");
    }

    /**
     * @param req
     * @param session
     * @return
     */
    @At
    @POST
    @Ok("json")
    public AjaxBean doLogin(HttpServletRequest req, HttpSession session) {
        log.info("user>doLogin");
        String uName = null, uPwd = null;
        String elements[] = null;
        try {
            uName = req.getParameter("name");
            uPwd = req.getParameter("pwd");
            elements = new String[]{uName,uPwd};
            if (userService.elementStrIsNull(elements)) {
                UserModel user = userService.login(elements[0], elements[1]);
                session.setAttribute(Constants.SESSION_KEY, user);
                return Ajax.ok().setData(user);
            }
        } catch (LogicException e) {
            return Ajax.fail().setMsg(e.getMessage());
        }
        return Ajax.fail().setMsg("登陆失败");
    }

    /**
     * @param session
     * @param req
     * @param user
     * @return
     */
    @At
    @Ok("json")
    public AjaxBean modfPwd(HttpSession session, HttpServletRequest req, @Attr(Constants.SESSION_KEY) UserModel user) {
        log.info("user>modfPwd");
        String npw = null, oldpw = null, npwa = null;
        String elements[] = null;
        try {
            npw = req.getParameter("npw");
            oldpw = req.getParameter("oldpw");
            npwa = req.getParameter("npwa");
            elements = new String[]{npw, oldpw, npwa};
            if (userService.elementStrIsNull(elements)) {
                UserModel outcome = userService.modfPwd(user, elements[0], elements[1], elements[2]);
                session.setAttribute(Constants.SESSION_KEY, outcome);
                return Ajax.ok().setData(outcome);
            }
        } catch (LogicException e) {
            return Ajax.fail().setMsg(e.getMessage());
        }
        return Ajax.fail().setMsg("修改失败");
    }

    /**
     * @param user
     * @return
     */
    @At
    @Ok("jsp:pages.back.main")
    public Map<String, Object> doMain(@Attr(Constants.SESSION_KEY) UserModel user) {
        log.info("user>toMain");
        Map<String, Object> map = new HashMap<String, Object>();
        if (user != null) {
            try {
                map.put("menuModels", userService.getMenus(user.getType()));
            } catch (Exception e) {
                log.info(TAG + e.getMessage());
            }
        }
        return map;
    }
}
